package fs

import (
	"crypto/sha1"
	"encoding/hex"
	"io"
	"log"
	"os"
	"path/filepath"
)

type Local struct{}

func (*Local) Upload(r io.Reader, name string) (n int64, sha1Hash string, err error) {
	f, err := os.Create(filepath.Join(STORAGE_ADDR, name))
	if nil != err {
		log.Println(err)
		return 0, "", err
	}
	defer f.Close()

	sh := sha1.New()
	tr := io.TeeReader(r, sh)
	n, err = io.Copy(f, tr)
	if nil != err {
		log.Println(err)
		return n, "", err
	}

	sha1Hash = hex.EncodeToString(sh.Sum(nil))

	return
}
func (*Local) Download(w io.Writer, name string) (n int64, err error) {
	f, err := os.Open(filepath.Join(STORAGE_ADDR, name))
	if nil != err {
		log.Println(err)
		return 0, err
	}
	defer f.Close()

	n, err = io.Copy(w, f)
	if nil != err {
		log.Println(err)
		return 0, err
	}

	return
}
